import { Plugin } from "esbuild";
import { BARE_IMPORT_RE, EXTERNAL_TYPES } from "../constants";
export function scanPlugin(deps: Set<string>): Plugin {
  return {
    name: "esbuild:scan-deps",
    setup(build) {
      build.onResolve({ filter: new RegExp(`\\.(${EXTERNAL_TYPES.join("|")})$`) }, (resolveInfo) => {
        // console.log("resolveInfo---1", resolveInfo)
        return {
          path: resolveInfo.path,
          // 打上 external 标记
          external: true,
        }
      })
      build.onResolve({ filter: BARE_IMPORT_RE }, (resolveInfo) => {
        // console.log("resolveInfo---2", resolveInfo)
        const { path: id } = resolveInfo;
        deps.add(id);
        return {
          path: id,
          external: true,
        };
      })
    }
  }
}